<?php
class Wall_View_Helper_ViewMoreM2B extends Zend_View_Helper_HtmlElement
{
  
  protected $_moreLength = 255; // Note: truncation at 255 + 4 = 259 (for " ...")
  protected $_maxLength = 1027;
  protected $_fudgesicles = 10;
  
  protected $_tag = 'span';
  protected function fixHTML($string,$tags="")// Tags: img,.... not contain <>
  {
      if ($pos = strrpos($string,'>'))
      {
          $string = substr($string,0,$pos+1);
      }
      elseif ($pos = strrpos($string,'<'))
      {
          $string = substr($string,0,$pos);
      }
      if ($tags)
      {
          $count = substr_count($string,"<$tags>")-substr_count($string,"</$tags>");
          if ($count>0)
          {
              for($i=0;$i<$count;$i++)
              {
                  $string.="</$tags>";
              }
          }
      }
      return $string;
      
  }

  public function viewMoreM2B($string)
  {
    $body = $string;
    $max_length = 500;
    if (strlen($string) < $max_length) {
        return $body;
    }    

    $string = preg_replace ('/<[^>]*>/', '', $string);
    $string = substr($string, 0, $max_length);
    $string = preg_replace ('/style=".*"/', '', $string);
    
    //$string = preg_replace ('/<[^>]*>/', '', $string);
    //$string = strip_tags($string, "<span><img>");    
    //$string = strip_tags($string);
    
    $content = '<br /><span class="view_more">';
    $content .= '<span>' . $string . '</span>'
       . '<a href="javascript:void(0);" class="view_more_link" style="margin-left: 5px;" onclick="var el = $(this).getParent().getChildren(); el[0].hide(); el[2].show(); el[3].show(); $(this).hide();">more</a>'
       . '<span style="display:none">' . $body . '</span>'
       . '<a href="javascript:void(0);" class="view_less_link" style="margin-left: 5px; display:none" onclick="var el = $(this).getParent().getChildren(); el[0].show(); el[2].hide(); el[1].show(); $(this).hide();">less</a>'
       . '</span>';

    /*
    $string_clear_html = strip_tags($string,"<img>");
    $count = substr_count($string_clear_html,"<img");
    if ($count)
    {
      $this->_moreLength += $count*70;
      $this->_maxLength += $count*70;
    }
    if( Engine_String::strlen($string_clear_html) <= $this->_moreLength + $this->_fudgesicles )
    {
        return $string;
    }
    if( Engine_String::strlen($string_clear_html) >= $this->_maxLength )
    {
        $string = $this->fixHTML(Engine_String::substr($string_clear_html, 0, $this->_maxLength)) . $this->view->translate('... &nbsp;');
    }
     
    $content .= '<'
    . $this->_tag
    . ' class="view_more"'
    . '>'
    . $this->fixHTML(Engine_String::substr($string_clear_html, 0, $this->_moreLength)) . $this->view->translate('... &nbsp;')
    . '<a class="view_more_link" href="javascript:void(0);" onclick="$(this).getParent().getNext().style.display=\'\';$(this).getParent().style.display=\'none\';">'.$this->view->translate('more').'</a>'
    . '</'
    . $this->_tag
    . '>'
    . '<'
    . $this->_tag
    . ' class="view_more"'
    . ' style="display:none;"'
    . '><br/>'
    . $string. ' &nbsp;'
    . '<a class="view_less_link" href="javascript:void(0);" onclick="$(this).getParent().getPrevious().style.display=\'\';$(this).getParent().style.display=\'none\';">'.$this->view->translate('less').'</a>'
    . '</'
    . $this->_tag
    . '>'
    ;*/

    return $content;
  }

  public function setMoreLength($length)
  {
    if( is_numeric($length) && $length > 0 )
    {
      $this->_moreLength = $length;
    }

    return $this;
  }

  public function setMaxLength($length)
  {
    if( is_numeric($length) && $length > 0 )
    {
      $this->_maxLength = $length;
    }

    return $this;
  }
}